{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "import math\n",
    "import time\n",
    "import cv2 as cv\n",
    "import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "testing_fg_root = '/home/zzl/dataset/Combined_Dataset/Test_set/test_fg_names.txt'\n",
    "testing_bg_root = '/home/zzl/dataset/Combined_Dataset/Test_set/test_bg_names.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ADOBE_ROOT = '/home/zzl/dataset/Combined_Dataset/Test_set/Adobe-licensed_images/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "BG_ROOT = '/home/zzl/dataset/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_path = '/home/zzl/dataset/Combined_Dataset/Test_set/Adobe-licensed_images/input/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def composite4(fg, bg, a, w, h):\n",
    "    fg = np.array(fg, np.float32)\n",
    "    bg = np.array(bg[0:h, 0:w], np.float32)\n",
    "    alpha = np.zeros((h, w, 1), np.float32)\n",
    "    alpha[:, :, 0] = a / 255.\n",
    "    comp = alpha * fg + (1 - alpha) * bg\n",
    "    comp = comp.astype(np.uint8)\n",
    "    return comp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "fg_files = [name for name in open(testing_fg_root).read().splitlines()]\n",
    "bg_files = [name for name in open(testing_bg_root).read().splitlines()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_bgs=20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_files = os.listdir(BG_ROOT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "bg_len = len(bg_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "50it [1:02:34, 71.08s/it]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for k, im_name in tqdm.tqdm(enumerate(fg_files)):\n",
    "\n",
    "    im = cv.imread(os.path.join(ADOBE_ROOT, 'fg',im_name))\n",
    "    a = cv.imread(os.path.join(ADOBE_ROOT, 'alpha', im_name), 0)\n",
    "\n",
    "\n",
    "    h, w = im.shape[:2]\n",
    "\n",
    "    bcount = 0\n",
    "    for i in range(num_bgs):\n",
    "\n",
    "        index = np.random.randint(bg_len)\n",
    "        bg_name = bg_files[index]\n",
    "\n",
    "        bg = cv.imread(os.path.join(BG_ROOT, bg_name))\n",
    "        bh, bw = bg.shape[:2]\n",
    "        wratio = float(w) / float(bw)\n",
    "        hratio = float(h) / float(bh)\n",
    "        ratio = wratio if wratio > hratio else hratio     \n",
    "        if ratio > 1:        \n",
    "            bg = cv.resize(src=bg, dsize=(math.ceil(bw * ratio), math.ceil(bh * ratio)), interpolation=cv.INTER_CUBIC)\n",
    "\n",
    "        out = composite4(im, bg, a, w, h)   \n",
    "        filename = out_path + im_name[:len(im_name)-4] + '_' + str(bcount) + '.png'\n",
    "\n",
    "        cv.imwrite(filename, out, [cv.IMWRITE_PNG_COMPRESSION, 9])        \n",
    "\n",
    "        bcount += 1\n",
    "            # print(k*num_bgs + bcount)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
